<template>
  <div>
    <div style="background:#FFF;padding:5px;">
      <el-button
        type="primary"
        size="small"
        @click="dialogImportVisible = true"
      >
        导入
      </el-button>
      <el-button type="success" size="small" @click="hanldeExport">
        导出
      </el-button>
    </div>
    <el-table
      :data="list"
      style="width: 100%"
      row-key="id"
      border
      lazy
      :load="load"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="分类名称" />
      <el-table-column prop="imageUrl" label="图标" #default="scope">
        <img
          v-if="scope.row.imageUrl"
          :src="scope.row.imageUrl"
          style="width:40px;height:40px"
        />
      </el-table-column>
      <el-table-column prop="orderNum" label="排序" />
      <el-table-column prop="status" label="状态" #default="scope">
        {{ scope.row.status === 1 ? '正常' : '不显示' }}
      </el-table-column>

      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="Operations">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
            修改
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 导入excel文件的对话框 -->
    <div>
      <el-dialog v-model="dialogImportVisible" title="导入" width="30%">
        <el-form label-width="120px">
          <el-form-item label="分类文件">
            <el-upload
              class="upload-demo"
              :action="uploadPath"
              name="excel"
              :on-success="onUploadSuccess"
              :headers="headers"
            >
              <el-button type="primary">上传</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { GetCategoriesByPid, ExportCatesExcel } from '@/api/category'
import { useApp } from '@/pinia/modules/app'
onMounted(() => {
  fetchData(0)
})
//导入对话框需要的属性和函数
const dialogImportVisible = ref(false)
const uploadPath = 'http://localhost:8501/admin/product/category/import'
const headers = { token: useApp().authorization.token }
const onUploadSuccess = (response, uploadFile) => {
  if (+response.code === 200) {
    ElMessage.success('导入成功')
    fetchData(0)
  } else {
    ElMessage.success(response.message)
  }

  dialogImportVisible.value = false
}
//导出按钮事件函数
const hanldeExport = () => {
  //.then 当promise结果正常时回调此方法
  ExportCatesExcel().then(res => {
    // 创建 Blob 对象，用于包含二进制数据
    const blob = new Blob([res])
    // 创建 a 标签元素，并将 Blob 对象转换成 URL
    const link = document.createElement('a')
    link.href = window.URL.createObjectURL(blob)
    // 设置下载文件的名称: 后端返回的数据需要原生处理 解析响应报文头 content-dispositon的值
    link.download = '分类数据.xlsx'
    // 模拟点击下载链接
    link.click()
  })
}

//表格需要的数据和函数
const list = ref([])
const load = async (row, treeNode, resolve) => {
  //row代表点击按钮所在行的json对象: row.id
  //resolve可以将加载到的数据列表渲染到点击按钮行的下面
  //   console.log(row, treeNode, resolve)
  const { data } = await GetCategoriesByPid(row.id)
  resolve(data)
}

const fetchData = async pid => {
  const { data } = await GetCategoriesByPid(pid)
  list.value = data
}
</script>
